class Controller {
    constructor(module, columns) {
        this.module = module;
        this.columns = columns;
    }
    init() {
        var currentModule = this.module;
        var that = this;
        this.url = `${parent.globalUrl}${currentModule}/list`
        $('#dg').datagrid({
            url: this.url,
            method: 'post',
            fit: true,
            pagination: true,
            columns: this.columns,
            toolbar: [{
                text: 'Add',
                iconCls: 'icon-add',
                handler: function() {
                    $('#dlg').dialog('open')
                }
            }, {
                text: '删除多个',
                iconCls: 'icon-add',
                handler: function() {
                    that: deleteRows();
                }
            }]

        });
    }
    clearFrom() {
        $('#ff').form('clear');
    }
    deleData(id) {
        var currentModule = this.module;
        $.message.confirm('提示', '你确定要删除该数据？', function(r) {
            if (r) {
                $.ajax({
                    url: `${parent.globalUrl}${currentModule}/data/` + id,
                    type: 'delete'
                }).then(res => {
                    $('#dg').datagrid('reload');
                })
            }
        })
    }
    editData(id) {
        var currentModule = this.module;
        $('#ff').from('load', `${parent.globalUrl}${currentModule}/data/` + id);
        $('#dlg').dialog('open');
    }
    deleteRows() {
        var currentModule = this.module;
        $.messager.confirm('提示', '你确认删除该数据?', function(r) {
            if (r) {
                var selections = $('#dg').datagrid('getSelections');
                if (selections.length > 0) {
                    var ids = [];
                    for (let i = 0; i < selections.length; i++) {
                        ids.push(selections[i]._id);
                    }
                    $.ajax({
                        url: `${parent.globalUrl}${currentModule}/data/`,
                        type: 'post',
                        data: {
                            ids: ids.toString()
                        }
                    }).then(res => {
                        $('#dg').datagrid('reload');
                    })
                }
            }
        });
    }
}